package com.qipay.user.service.impl;

import com.qipay.baas.model.User;
import com.qipay.mapper.UserMapper;
import com.qipay.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getByName(String name) {
        return userMapper.findFirstByName(name);
    }

    @Override
    public Long count(String name) {
        name = StringUtils.isBlank(name) ? "" : name;
        return userMapper.countAll(name);
    }

    @Override
    public List<User> page(String name, int no, int size) {
        name = StringUtils.isBlank(name) ? "" : name;
        no = no <= 0 ? 1 : no;
        size = size <= 0 ? 10 : size;
        return userMapper.page(name, (no - 1) * size, size);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    @Transactional
    public int updatePassword(long uid, String pwd) {
        User user = new User();
        user.setId((int) uid);
        user.setPwd(pwd);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional
    public int updateLevel(long uid, int level) {
        User user = new User();
        user.setId((int) uid);
        user.setLevel(level);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getByPK(long uid) {
        return userMapper.selectByPrimaryKey((int)uid);
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> findByLevel(int level) {

        return userMapper.selectByLevel(level);
    }

}
